//
//  discountCell.m
//  UI_HLJ_Frame
//
//  Created by  毕宏伟 on 15/6/30.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "discountCell.h"
#define WIDTH self.contentView.frame.size.width
#import "AFNetworking.h"
#import "discountModel.h"
#import "UIImageView+WebCache.h"
#import "DiscountCollectionViewCell.h"
#import "disCountdetailViewController.h"
@implementation discountCell
- (void)dealloc
{
    [_titleLabel release];
    [_marketlabel release];
    [_actualLabel release];
    [_scrollImage release];
    [_scrollview release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
            [self createData];
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize =CGSizeMake(self.contentView.frame.size.width,self.contentView.frame.size.height);
    
    //设置水平滚动
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView.dataSource =self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    [_collectionView release];
    
    
    //重用collectionview注册
    [self.collectionView registerClass:[DiscountCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];

}


//collection协议方法返回item的个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.discountArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DiscountCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    discountModel *dis = self.discountArr[indexPath.row];
    [cell.disImage sd_setImageWithURL:[NSURL URLWithString:dis.cover_path]];
    cell.titleLabel.text = dis.title;
    cell.marketLabel.text = [dis.market_price stringValue];
    cell.disLabel.text =[dis.actual_price stringValue];
    
    NSString *str =[dis.market_price stringValue];
    NSUInteger length = [str length];
    
    
    //设置折扣价格
    NSMutableAttributedString *lenstr = [[NSMutableAttributedString alloc]initWithString:str];
    [lenstr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, length)];
    [lenstr addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, length)];
    
    [cell.marketLabel setAttributedText:lenstr];
    
    
    
    return cell;
    
}

//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    [self.delegatenew tiaozhuanyemian];
//}





- (void)createData
{
    
    self.discountArr = [NSMutableArray array];
    self.disPicArr = [NSMutableArray array];
AFHTTPRequestOperationManager *discountmanager = [AFHTTPRequestOperationManager manager];

[discountmanager GET:@"http://hunliji.com/v1/api/app/works/discount" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
    NSDictionary *dic = responseObject;
    NSArray *worksArr = dic[@"works"];
    for (NSDictionary *worksdic in worksArr) {
        discountModel *dis = [[discountModel alloc]init];
        [dis setValuesForKeysWithDictionary:worksdic];
        [self.disPicArr addObject:dis.cover_path];
        [self.discountArr addObject:dis];
        
        [dis release];
    }
    
//    NSLog(@"数据解析成功");
    [self.collectionView reloadData];
    
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
}];

}






- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
